package user;

import book.Book;
import book.Library;
import utils.PermisionExeption;
import constant.Constant;

public class ProxyUser {
    private Library libarary;
    public User realuser;
    public ProxyUser(User user){
        this.realuser = user;
        libarary = Library.getLibrary();
    }
    public User getRealuser(){
        return realuser;
    }
    public int dispaly() {
        return this.realuser.dispaly();
    }

    //=========================管理员==========================
    private void checkRealUserWehtherAdimalUser(String msg){
        if(!(this.realuser instanceof AdimalUser)){
            throw new PermisionExeption(msg);
        }
    }
    public void addBook() {
        System.out.println("代理类addbook方法执行");

        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限添加书籍");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
       ((AdimalUser)(this.realuser)).addBook();
    }
    //图书修改 ⽀持修改书名 作者 类别
    public void updateBook() {
        System.out.println("代理类updateBook方法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限更新书籍");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).updateBook();
    }
    //删除书籍
    public void removeBook() {
        System.out.println("代理类removeBook方法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限查看借阅次数");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).removeBook();
    }
    //统计每本书的借阅次数
    public void borrowCount() {
        System.out.println("代理类borrowCount方法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限添加书籍");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).borrowCount();
    }
    //查询最受欢迎的前n本书
    public void generateBook() {
        System.out.println("代理类generateBook方法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限查询最受欢迎的书");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("代理类checkInventoryStatus法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限查看库存状态");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).checkInventoryStatus();
    }
    //按照类别 统计图书
    public void categorizeBooksByCategory() {
        System.out.println("代理类categorizeBooksByCategory方法执行");
    }

    //按照作者统计图书
    public void categorizeBooksByAuthor() {
        System.out.println("代理类categorizeBooksByAuthor方法执行");
    }
    //并移除上架超过⼀年的图书
    public void checkAndRemoveOldBooks() {
        System.out.println("代理类heckAndRemoveOldBooks方法执行");
        try{
            checkRealUserWehtherAdimalUser("普通用户没有权限移除旧书");
        }catch (PermisionExeption e){
            e.printStackTrace();
        }
        ((AdimalUser)(this.realuser)).checkAndRemoveOldBooks();
    }
    //===============普通===================//
    private void checkRealUserWehtherNormalUser(String expertionMessge){
        if(!(this.realuser instanceof NormalUser)){
            throw new PermisionExeption(expertionMessge);
        }
    }
    public void borrowBook() {
        checkRealUserWehtherNormalUser("管理员没有权限借阅书籍");
        ((NormalUser)(this.realuser)).borrowBook();
    }

    //归还图书
    public void returnBook() {
        System.out.println("代理类returnBook方法执行");
        checkRealUserWehtherNormalUser("管理员没有权限归还书籍");
        ((NormalUser)(this.realuser)).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        System.out.println("代理类viewBorrowHistory方法执行");
        checkRealUserWehtherNormalUser("管理员没有权限归还书籍");
        ((NormalUser)(this.realuser)).viewBorrowHistory();
    }

    public void handleOperation(int choice) {
        if(this.realuser instanceof AdimalUser){
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    libarary.searchBaook();
                    break;
                case Constant.DISPLAY_BOOK:
                    libarary.diaplayBook();
                    break;
                case Constant.EXIT:
                    libarary.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                default:
                    break;

            }
        }else{
            switch (choice){
                case Constant.SEARCH_BOOK:
                    libarary.searchBaook();
                    break;
                case Constant.DISPLAY_BOOK:
                    libarary.diaplayBook();
                    break;
                case Constant.EXIT:
                    libarary.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                default:
                    break;
            }
        }
    }
}
